package net.wofly.right.validator;


import net.wofly.right.domain.Organization;
import net.wofly.right.domain.User;
import net.wofly.right.domain.UserRole;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

public class RightValidator {
    
    /**
     * 换行符
     */
    private static String lineSeparator = System.getProperty("line.separator");
    
    public static void validateUserID(StringBuffer stringBuffer, String userID) {
        if (StringUtils.isEmpty(userID)) {
            stringBuffer.append("用户ID不能为空！").append(lineSeparator);
        }
    }
    
    public static String validateAddUserParams(User user) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == user) {
            stringBuffer.append("user 不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(user.getOrganizationID())) {
                stringBuffer.append("员工的组织机构ID不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(user.getMobile())) {
                stringBuffer.append("员工的手机号码不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(user.getPassword())) {
                stringBuffer.append("员工的密码不能为空").append(lineSeparator);
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateAddOrganizationParams(Organization organization) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == organization) {
            stringBuffer.append("organization 不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(organization.getOrganizationName())) {
                stringBuffer.append("组织机构的名称不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(organization.getWarehouseFlag())) {
                stringBuffer.append("组织机构的仓库标识不能为空").append(lineSeparator);
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateUpdateOrganizationParams(Organization organization) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == organization) {
            stringBuffer.append("organization 不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(organization.getOrganizationID())) {
                stringBuffer.append("组织机构ID不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(organization.getOrganizationName())) {
                stringBuffer.append("组织机构的名称不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(organization.getWarehouseFlag())) {
                stringBuffer.append("组织机构的仓库标识不能为空").append(lineSeparator);
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateGetUserByOrganizationParams(String organizationID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(organizationID)) {
            stringBuffer.append("组织机构ID不能为空").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateSetUserRoleParams(UserRole userRole) {
        StringBuffer stringBuffer = new StringBuffer();
        if (userRole == null) {
            stringBuffer.append("userRole 不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(userRole.getSetUserID())) {
                stringBuffer.append("setUserID 不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(userRole.getUserID())) {
                stringBuffer.append("userID 不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(userRole.getRoleID())) {
                stringBuffer.append("roleID 不能为空").append(lineSeparator);
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateUpdateUserParams(User user) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == user) {
            stringBuffer.append("user 不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(user.getUserID())) {
                stringBuffer.append("员工的ID不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(user.getOrganizationID())) {
                stringBuffer.append("员工的组织机构ID不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(user.getMobile())) {
                stringBuffer.append("员工的手机号码不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(user.getPassword())) {
                stringBuffer.append("员工的密码不能为空").append(lineSeparator);
            }
        }
        return stringBuffer.toString();
    }

    public static String validateGetRightConfigByRoleIDParams(String roleID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == roleID) {
            stringBuffer.append("roleID不能为空").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateGetCustomerForOrder(String orderType, String saleUserID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == orderType) {
            stringBuffer.append("订单类型（orderType）不能为空").append(lineSeparator);
        }
        if (null == saleUserID) {
            stringBuffer.append("业务经理ID（saleUserID）不能为空").append(lineSeparator);
        }
        return stringBuffer.toString();
    }

    public static String validateImportCustomerExcelParams(MultipartFile file, String userID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == file) {
            stringBuffer.append("文件（file）不能为空！").append(lineSeparator);
        }

        //获取文件名
        String name = file.getOriginalFilename();
        //进一步判断文件是否为空（即判断其大小是否为0或其名称是否为null）
        long size=file.getSize();
        if(name==null || ("").equals(name) && size == 0) {
            stringBuffer.append("文件（file）不能为空！").append(lineSeparator);
        } else if (file.getSize() > 10 * 1024 * 1024 * 8) {
            stringBuffer.append("文件最大为10M！").append(lineSeparator);
        }

        if (StringUtils.isEmpty(userID)) {
            stringBuffer.append("用户ID（userID）不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
}
